package com.enation.app.shop.front.policyInsurance.appController;

import com.enation.app.shop.front.api.inpolicy.entity.InPolicy;
import com.enation.app.shop.front.policyInsurance.appController.InPolicyUtils.InsPolicyUtils;
import com.enation.app.shop.front.policyInsurance.entity.Farme;
import com.enation.app.shop.front.policyInsurance.service.InsuranceCheckMapper;
import com.enation.app.shop.front.policyInsurance.service.InsuranceProtOrdMapper;
import com.enation.framework.action.JsonResult;
import com.enation.framework.util.JsonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保险端保单单管理
 */
@Scope("prototype")
@Controller
@RequestMapping("app/InsurancProtManage")
public class InsurancePortOrderController {
    @Autowired
    private InsuranceProtOrdMapper inPolicyProtOrdManager;

    @Autowired
    private InsuranceCheckMapper insuranceCheckMapper;
    /**
     * 保险端-查找保单
     * @param str
     * @return
     */
    @ResponseBody
    @RequestMapping("/getInPolicyListInfo")
    public JsonResult getInPolicyListInfo(String str){
        //根据保单状态查找
        if(str!=null){
            if(str.equals("ALL")){
                try {
                    List<InPolicy> inPolicyList = inPolicyProtOrdManager.getAllInPolicyOrderInfo();
                    List list = InsPolicyUtils.chageInPolicyStatus(inPolicyList);
                    if(list.size()>=0){
                        return JsonResultUtil.getObjectMessageJson(list,"获取数据成功!");
                    }
                }catch (Exception e){
                    return JsonResultUtil.getErrorJson("获取失败!");
                }
            }
            if(str.equals("DSH")){
                try {
                    List<InPolicy> inPolicyList = inPolicyProtOrdManager.getUnExplorInPolicyOrderInfo();
                    List list = InsPolicyUtils.chageInPolicyStatus(inPolicyList);
                    if(list.size()>=0){
                        return JsonResultUtil.getObjectMessageJson(list,"获取数据成功!");
                    }
                }catch (Exception e){
                    return JsonResultUtil.getErrorJson("获取失败!");

                }
            }
            if(str.equals("XCKC")){
                try {
                    List<InPolicy> inPolicyList = inPolicyProtOrdManager.getCheckInPolicyOrderInfo();
                    List list = InsPolicyUtils.chageInPolicyStatus(inPolicyList);
                    if(list.size()>=0){
                        return JsonResultUtil.getObjectMessageJson(list,"获取数据成功!");
                    }
                }catch (Exception e){
                    return JsonResultUtil.getErrorJson("获取失败!");
                }
            }
            if(str.equals("YWC")){
                try {
                    List<InPolicy> inPolicyList = inPolicyProtOrdManager.getOverInPolicyOrder();
                    List list = InsPolicyUtils.chageInPolicyStatus(inPolicyList);
                    if(list.size()>=0){
                        return JsonResultUtil.getObjectMessageJson(list,"获取数据成功!");
                    }
                }catch (Exception e){
                    return JsonResultUtil.getErrorJson("获取失败!!");
                }
            }
        }
            return JsonResultUtil.getErrorJson("请求错误!");
        }

    /**
     * 获取报保详情
     * @param
     * @return
     */
        @ResponseBody
        @RequestMapping("/getInpoLicyDetail")
        public JsonResult getInpoLicyDetail(Integer inPolicy_id){
            if(inPolicy_id==null){
                return JsonResultUtil.getErrorJson("获取数据失败!");
            }
            try {
                InPolicy inPolicy = inPolicyProtOrdManager.getInPolicyDeatil(inPolicy_id);
                if(inPolicy.getCase_date()==null){
                    inPolicy.setCase_date(new Timestamp(System.currentTimeMillis()));
                }
                if(inPolicy!=null){
                    return JsonResultUtil.getObjectMessageJson(inPolicy,"获取数据成功!");
                }else {
                    return JsonResultUtil.getErrorJson("请求异常!");
                }
            }catch (Exception e){
                return JsonResultUtil.getErrorJson("获取数据失败!");
            }
    }

    /**
     * 获取养殖场信息
     * @param inPolicy_id
     * @param userid
     * @return
     */
    @ResponseBody
    @RequestMapping("/getFarmeInfo")
    public JsonResult getFarmeInfo(Integer inPolicy_id,Integer userid){
        try {
            Map map=new HashMap();
            List inPolicyNumberlist=new ArrayList();
            InPolicy inPolicy= inPolicyProtOrdManager.getInPolicyDeatil(inPolicy_id);
            List<InPolicy> inPolicyList = inPolicyProtOrdManager.getHistoryOrder(userid);
            for(InPolicy inPolicy1:inPolicyList){
                inPolicyNumberlist.add(inPolicy.getWarranty_number());
            }
            Farme farme=new Farme();
            farme.setFarme_stock(10);
            farme.setInpolicy_id(inPolicy_id);
            farme.setParturition_status(1);
            farme.setParturition_status(1);
            farme.setFarme_livestock(inPolicy.getItem().substring(inPolicy.getItem().length()-1,inPolicy.getItem().length()));
            farme.setNear_record_date(new Timestamp(System.currentTimeMillis()));
            farme.setNear_immune_date(new Timestamp(System.currentTimeMillis()));
            map.put("breeding_farm_azimuth",inPolicy.getBreeding_farm_azimuth());
            map.put("inPolicyNumberlist",inPolicyNumberlist);
            map.put("farme",farme);
            return JsonResultUtil.getObjectMessageJson(map,"");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("");
        }
    }
}